<?php
    $xtpl_edit      = new XTemplate("view/order/edit.htm");
    $id             = $_GET['id'];
    
    $title  = "Thông tin đơn hàng";
    $total  = 0;
   
    $query  = "SELECT * FROM orders WHERE id_o='" . $id ."'";
    $re     = mysql_query($query);
    $row    = mysql_fetch_array($re);
    $sqldate    = strtotime($row['date']);
    $date       = date('d-m-Y', $sqldate);
    
    $query  = "SELECT * FROM customer WHERE id_c='" . $row['id_c'] . "'";
    $res    = mysql_query($query);
    $rows   = mysql_fetch_array($res);
    
    $xtpl_edit      -> assign('fullname', $rows['fullname']);
    $xtpl_edit      -> assign('email', $rows['email']);
    $xtpl_edit      -> assign('address', $rows['address']);
    $xtpl_edit      -> assign('phone', $rows['phone']);
    $xtpl_edit      -> assign('date', $date);

    $sql    = "SELECT * FROM order_detail WHERE id_o='" . $row['id_o'] . "'";
    $result = mysql_query($sql);

    while($row_o  = mysql_fetch_array($result)){
        $sqlp   = "SELECT * FROM product WHERE id_p='" . $row_o['id_p'] . "'";
        $result2= mysql_query($sqlp);
        $row_p  = mysql_fetch_array($result2);
        
        $xtpl_edit  -> assign('milkname', $row_p['milkname']);
        $xtpl_edit  -> assign('number', $row_o['number']);
        $xtpl_edit  -> assign('cost', $row_o['cost']);
        $xtpl_edit  -> insert_loop('EDIT.TABLES', array('EDIT'=>$row));
        $total  = $total + $row_o['cost']*$row_o['number'];
    }
    
    if(isset($_POST['submit'])){
        $status = 1;
        $sql    = "UPDATE orders SET status='" . $status . "' WHERE id_o='" . $id . "'";
        $result = mysql_query($sql);
        if($result){
            $error = "Đã hoàn thành đơn hàng";
            $xtpl_edit  -> assign('error', $error);
        }
    }
    
    if(isset($_POST['cancel'])){
        header("location:index.php?page=orders");
    }
    
    $xtpl_edit      -> assign('total', $total);
    $xtpl_edit      -> parse("EDIT");
    $content        = $xtpl_edit -> text("EDIT");
?>